﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class PagerControl : UserControl
    {
        protected LinkButton btnFirst;
        protected LinkButton btnLast;
        protected LinkButton btnNext;
        protected LinkButton btnPrev;
        private object dataSource;
        protected DropDownList ddlPages;
        private bool inited = false;
        private int itemCount = 0;
        private int itemLength = 0;
        protected Label lblPage;
        private int pageCount = 0;
        private string pagedControlId;
        private int pageIndex = 0;
        private int pageSize = 10;
        private Repeater repeater;
        protected HtmlGenericControl spanPager;
        private int startIndex = 0;

        public event Shanda.DevFx.Security.Web.Pages.PagerPageEventHandler PageIndexChanged;

        private void btnFirst_Click(object sender, EventArgs e)
        {
            Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs args = new Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs(0, this.pageSize);
            this.OnPageIndexChanged(args);
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            this.InitData();
            Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs args = new Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs(this.pageCount - 1, this.pageSize);
            this.OnPageIndexChanged(args);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.InitData();
            Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs args = new Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs(this.pageIndex + 1, this.pageSize);
            this.OnPageIndexChanged(args);
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            this.InitData();
            Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs args = new Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs(this.pageIndex - 1, this.pageSize);
            this.OnPageIndexChanged(args);
        }

        private void CalcPage()
        {
            this.pageCount = this.itemCount / this.pageSize;
            if ((this.itemCount % this.pageSize) != 0)
            {
                this.pageCount++;
            }
            if ((this.pageIndex >= this.pageCount) && (this.pageCount > 0))
            {
                this.pageIndex = this.pageCount - 1;
            }
            this.startIndex = this.pageIndex * this.pageSize;
            if ((this.pageIndex < (this.pageCount - 1)) || ((this.itemCount > 0) && ((this.itemCount % this.pageSize) == 0)))
            {
                this.itemLength = this.pageSize;
            }
            else
            {
                this.itemLength = this.itemCount % this.pageSize;
            }
        }

        private void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = this.ddlPages.get_SelectedValue();
            try
            {
                Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs args = new Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs(int.Parse(s), this.pageSize);
                this.OnPageIndexChanged(args);
            }
            catch
            {
            }
        }

        private void InitData()
        {
            if (!this.inited)
            {
                if (this.pagedControlId != null)
                {
                    this.repeater = Shanda.DevFx.Security.Web.Pages.ControlHelper.FindControl(this, this.pagedControlId) as Repeater;
                }
                string s = this.ddlPages.get_SelectedValue();
                this.pageIndex = 0;
                try
                {
                    this.pageIndex = int.Parse(s);
                }
                catch
                {
                }
                this.pageCount = this.ddlPages.get_Items().get_Count();
                this.inited = true;
            }
        }

        private void InitializeComponent()
        {
            base.add_Load(new EventHandler(this.Page_Load));
            this.btnFirst.add_Click(new EventHandler(this.btnFirst_Click));
            this.btnPrev.add_Click(new EventHandler(this.btnPrev_Click));
            this.btnNext.add_Click(new EventHandler(this.btnNext_Click));
            this.btnLast.add_Click(new EventHandler(this.btnLast_Click));
            this.ddlPages.add_SelectedIndexChanged(new EventHandler(this.ddlPages_SelectedIndexChanged));
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            this.InitData();
            if (this.itemCount > 0)
            {
                this.spanPager.set_Visible(true);
            }
            else
            {
                this.spanPager.set_Visible(false);
            }
            this.CalcPage();
            if (this.pageIndex == 0)
            {
                this.btnFirst.set_Enabled(false);
                this.btnPrev.set_Enabled(false);
            }
            else
            {
                this.btnFirst.set_Enabled(true);
                this.btnPrev.set_Enabled(true);
            }
            if (this.pageIndex >= (this.pageCount - 1))
            {
                this.btnNext.set_Enabled(false);
                this.btnLast.set_Enabled(false);
            }
            else
            {
                this.btnNext.set_Enabled(true);
                this.btnLast.set_Enabled(true);
            }
            this.lblPage.set_Text(string.Format("当前：第{0}/{1}页，共 {2} 项，每页 {3} 项", new object[] { this.pageIndex + 1, this.pageCount, this.itemCount, this.pageSize }));
            this.ddlPages.get_Items().Clear();
            for (int i = 0; i < this.pageCount; i++)
            {
                int num2 = i + 1;
                ListItem item = new ListItem(num2.ToString(), i.ToString());
                if (i == this.pageIndex)
                {
                    item.set_Selected(true);
                }
                this.ddlPages.get_Items().Add(item);
            }
            this.repeater.set_DataSource(this.dataSource);
            this.repeater.DataBind();
            if (this.pageCount <= 1)
            {
                this.spanPager.set_Visible(false);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        protected void OnPageIndexChanged(Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs e)
        {
            if ((this.repeater != null) && (this.PageIndexChanged != null))
            {
                this.PageIndexChanged(this, e);
            }
        }

        private void Page_Load(object sender, EventArgs e)
        {
            this.InitData();
        }

        public object DataSource
        {
            get
            {
                return this.dataSource;
            }
            set
            {
                this.dataSource = value;
            }
        }

        public int ItemCount
        {
            get
            {
                return this.itemCount;
            }
            set
            {
                if (value >= 0)
                {
                    this.itemCount = value;
                    this.CalcPage();
                }
            }
        }

        public int ItemLength
        {
            get
            {
                return this.itemLength;
            }
        }

        public int PageCount
        {
            get
            {
                return this.pageCount;
            }
        }

        public string PagedControlID
        {
            get
            {
                return this.pagedControlId;
            }
            set
            {
                this.pagedControlId = value;
            }
        }

        public int PageIndex
        {
            get
            {
                return this.pageIndex;
            }
            set
            {
                if (value >= 0)
                {
                    this.pageIndex = value;
                    this.CalcPage();
                }
            }
        }

        public int PageSize
        {
            get
            {
                return this.pageSize;
            }
            set
            {
                if (value > 1)
                {
                    this.pageSize = value;
                    this.CalcPage();
                }
            }
        }

        public int StartIndex
        {
            get
            {
                return this.startIndex;
            }
        }
    }
}

